package com.example.demo.scheduledtask;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.config.ErpConfig;
import com.example.demo.mapper.PoMpsinfoMapper;
import com.example.demo.mapper.WmsPurchaseMapper;
import com.example.demo.utils.SoapXmlUtil;
import com.example.demo.vo.CodeHoldcntVo;
import com.example.demo.vo.XmlResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author : Zhang.yt
 * @date : 2024/7/29
 */
@Component
@Slf4j
@DS("wms")
public class CodeHoldcnt {
    @Resource
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Autowired
    private SoapXmlUtil soapXmlUtil;

    @Autowired
    PoMpsinfoMapper poMpsinfoMapper;

    @Resource
    private ErpConfig erpConfig;
    /**
     * 物料冻结
     */
    @Scheduled(fixedDelay = 120 * 1000)//开启定时同步
//    //@Async(value = "taskExecutor")
    public synchronized void CodeHoldcnt() {
        CodeHoldcntVo codeHoldcntVo = wmsPurchaseMapper.getCodeHoldcnt();
        if (ObjectUtil.isEmpty(codeHoldcntVo) || ObjectUtil.isEmpty(codeHoldcntVo.getImaa001())) {
            return;
        }
        String resMessage = soapXmlUtil.getSoapXml("CodeHoldcnt", "imaa_t", codeHoldcntVo);
        try {
            //转换为soap消息
            log.info(resMessage);
            //log.info("采购入库单回调：{}",resMessage);
            //发送soap消息，并解析结果
            String result;
            try {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.getUrl());
            } catch (Exception e) {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.URL2);
            }
            XmlResp xmlResp = soapXmlUtil.analysisXml(result);
            log.info("料件批次冻结返回->{}", JSONObject.toJSONString(xmlResp));
            if (xmlResp.getCode().equals("0")) {
                //保存进erp数据表
                wmsPurchaseMapper.updateCpbarcodelistDj(codeHoldcntVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
